//
//  CHNewFeatureView.swift
//  status
//
//  Created by 彳亍 on 2017/3/23.
//  Copyright © 2017年 彳亍. All rights reserved.
//

import UIKit
import pop

class CHNewFeatureView: UIView {

    /// 滚动视图
    fileprivate lazy var scrollView = UIScrollView()
    /// UIPageControl
    fileprivate lazy var pageControl = UIPageControl()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        // 设置UI
        setupUI()
        //设置滚动视图
        setupScrollView()
    
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

// MARK: - UI
extension CHNewFeatureView {
    
    /// 设置UI
    fileprivate func setupUI() {
        // 添加视图
        addSubview(scrollView)
        addSubview(pageControl)
        
        // 布局
        scrollView.snp.makeConstraints { (ConstraintMaker) in
            ConstraintMaker.edges.equalToSuperview()
        }
        
        pageControl.snp.makeConstraints { (ConstraintMaker) in
            ConstraintMaker.centerX.equalToSuperview()
            ConstraintMaker.centerY.equalToSuperview().offset(200)
        }
        
    }


    
    /// 设置滚动视图
    fileprivate func setupScrollView() {
        // 1. 设置参数
        let imageCount = 4
        let rect = UIScreen.main.bounds
        
        scrollView.contentSize = CGSize(width: rect.width * CGFloat(imageCount + 1), height: rect.height)
        scrollView.delegate = self
        scrollView.isPagingEnabled = true
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.bounces = false
        
        pageControl.numberOfPages = imageCount
        pageControl.currentPageIndicatorTintColor = UIColor.orange
        pageControl.pageIndicatorTintColor = UIColor.darkGray
        
        // 2. 添加图片
        for i in 1...imageCount {
            let imageName = "new_feature_\(i)"
            let imageView = UIImageView(image: UIImage(named:imageName))
            imageView.frame = rect.offsetBy(dx: CGFloat(i-1)*rect.width, dy: 0)
            scrollView.addSubview(imageView)
        }
        
        // 3. enterButton
        let enterButton = UIButton(type: UIButtonType.custom)
        enterButton.setBackgroundImage(UIImage(named:"new_feature_finish_button"), for: UIControlState.normal)
        enterButton.setBackgroundImage(UIImage(named:"new_feature_finish_button_highlighted"), for: UIControlState.highlighted)
        enterButton.setTitle("进入微博", for: UIControlState.normal)
        enterButton.addTarget(self, action: #selector(enterMainController), for: UIControlEvents.touchUpInside)
        
        guard let lastView = scrollView.subviews.last else {
            return
        }
        // 开启交互
        lastView.isUserInteractionEnabled = true
        // 添加进入按钮
        lastView.addSubview(enterButton)
        
        enterButton.snp.makeConstraints { (ConstraintMaker) in
            ConstraintMaker.centerX.equalToSuperview()
            ConstraintMaker.centerY.equalToSuperview().offset(100)
        }
        
    }

}


// MARK: - 监听方法
extension CHNewFeatureView {
    
    /// 进入主控制器
    @objc fileprivate func enterMainController(){
        // 记录
        UserDefaults.standard.set(true, forKey: CHUserNotFirstInstallKey)
        //移除
        removeFromSuperview()
    }

}

// MARK: - UIScrollViewDelegate
extension CHNewFeatureView:UIScrollViewDelegate {

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        let currentPage = Int(scrollView.contentOffset.x / UIScreen.main.bounds.width + 0.5)
        pageControl.currentPage = currentPage
        
        // 如果当前视图为 scrollView 最后一个子视图 则隐藏 pageControl
        pageControl.isHidden = (currentPage == scrollView.subviews.count)
    }
    
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        
        let currentPage = Int(scrollView.contentOffset.x / UIScreen.main.bounds.width)
        
        currentPage == scrollView.subviews.count ?  enterMainController() : ()
        
    }

}




